<template>
	<div class="sys-pos-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label="$t('system.position.posName')">
					<el-input v-model="state.queryParams.name" :placeholder="$t('system.position.posName')" clearable />
				</el-form-item>
				<el-form-item :label="$t('system.position.posCode')">
					<el-input v-model="state.queryParams.code" :placeholder="$t('system.position.posCode')" clearable />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysPos:list'"> {{ $t('system.btn.query') }} </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery"> {{ $t('system.btn.reset') }} </el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="openAddPos" v-auth="'sysPos:addPos'"> {{ $t('system.btn.add') }} </el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 5px">
			<el-table :data="state.posData" style="width: 100%" v-loading="state.loading" border>
				<el-table-column type="index" :label="$t('system.dict.index')" width="55" align="center" />
				<el-table-column prop="name" :label="$t('system.position.posName')" align="center" show-overflow-tooltip />
				<el-table-column prop="code" :label="$t('system.position.posCode')" align="center" show-overflow-tooltip />
				<el-table-column prop="userList" :label="$t('system.position.employeeCount')" width="120" align="right" show-overflow-tooltip >
					<template #default="scope">{{ scope.row.userList?.length}}</template>
				</el-table-column>
				<el-table-column prop="userList" :label="$t('system.position.userDetail')" width="120" align="center" show-overflow-tooltip >
					<template #default="scope">
						<el-popover placement="bottom" width="280" trigger="hover" v-if="scope.row.userList?.length">
							<template #reference>
								<el-text type="primary" class="cursor-default">
									<el-icon><ele-InfoFilled /></el-icon>{{ $t('system.position.userDetail') }}
								</el-text>
							</template>
							<el-table :data="scope.row.userList" stripe border>
								<el-table-column type="index" :label="$t('system.dict.index')" width="55" align="center" />
								<el-table-column prop="account" :label="$t('system.user.account')" />
								<el-table-column prop="realName" :label="$t('system.user.realName')" />
							</el-table>
						</el-popover>
					</template>
				</el-table-column>
				<el-table-column prop="sort" :label="$t('system.dict.sort')" width="70" align="center" show-overflow-tooltip />
				<el-table-column :label="$t('system.dict.status')" width="70" align="center" show-overflow-tooltip>
					<template #default="scope">
						<g-sys-dict v-model="scope.row.isDisable" code="StatusEnum" />
					</template>
				</el-table-column>
				<el-table-column :label="$t('system.dict.modifyRecord')" width="100" align="center" show-overflow-tooltip>
					<template #default="scope">
						<ModifyRecord :data="scope.row" />
					</template>
				</el-table-column>
				<el-table-column :label="$t('system.dict.operate')" width="210" fixed="right" align="center" show-overflow-tooltip>
					<template #default="scope">
						<el-button icon="ele-Edit" text type="primary" @click="openEditPos(scope.row)" v-auth="'sysPos:updatePos'"> {{ $t('system.btn.edit') }} </el-button>
						<el-button icon="ele-Delete" text type="danger" @click="delPos(scope.row)" v-auth="'sysPos:deletePos'"> {{ $t('system.btn.delete') }} </el-button>
						<el-button icon="ele-CopyDocument" text type="primary" @click="openCopyMenu(scope.row)" v-auth="'sysPos:addPos'"> {{ $t('system.dict.copy') }} </el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>

		<EditPos ref="editPosRef" :title="state.editPosTitle" @handleQuery="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="sysPos">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditPos from '/@/views/system/position/component/editPos.vue';
import ModifyRecord from '/@/components/table/modifyRecord.vue';

import {apiSysPosDeletePost,apiSysPosListGet} from '/@/api-services/api';

const editPosRef = ref<InstanceType<typeof EditPos>>();
const state = reactive({
	loading: false,
	posData: [] as Array<any>,
	queryParams: {
		name: undefined,
		code: undefined,
	},
	editPosTitle: '',
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	var res = await apiSysPosListGet(state.queryParams.name, state.queryParams.code);
	state.posData = res.data.data ?? [];
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.name = undefined;
	state.queryParams.code = undefined;
	handleQuery();
};

// 打开新增页面
const openAddPos = () => {
	state.editPosTitle = $t('system.btn.add') + $t('system.position.title');
	editPosRef.value?.openDialog({ isDisable: 0, sort: 100 });
};

// 打开编辑页面
const openEditPos = (row: any) => {
	state.editPosTitle = $t('system.btn.edit') + $t('system.position.title');
	editPosRef.value?.openDialog(row);
};

// 打开复制页面
const openCopyMenu = (row: any) => {
	state.editPosTitle = $t('system.dict.copy') + $t('system.position.title');
	var copyRow = JSON.parse(JSON.stringify(row)) as any;
	copyRow.id = 0;
	copyRow.name = '';
	editPosRef.value?.openDialog(copyRow);
};

// 删除
const delPos = (row: any) => {
	ElMessageBox.confirm($t('system.btn.deleteConfirm') + `：【${row.name}】?`, $t('system.btn.confirm'), {
		confirmButtonText: $t('system.btn.confirm'),
		cancelButtonText: $t('system.btn.undo'),
		type: 'warning',
	})
			.then(async () => {
				await apiSysPosDeletePost({ id: row.id });
				handleQuery();
				ElMessage.success($t('system.btn.delete') + $t('system.btn.success'));
			})
			.catch(() => {});
};
</script>
